package com.ftg.learn.pattern;

import org.apache.ibatis.exceptions.ExceptionFactory;
import org.apache.ibatis.executor.ErrorContext;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.defaults.DefaultSqlSession;
import org.apache.ibatis.session.defaults.DefaultSqlSessionFactory;
import org.apache.ibatis.transaction.Transaction;
import org.apache.ibatis.transaction.TransactionFactory;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Objects;

/**
 * @author kangjx
 * @createTime 2022/11/28
 */
public class CoatFactory {

    private static final String DEFAULT_SIZE = "XL";
    private static final String DEFAULT_COLOR = "blue";

    public static Coat getCoat(String type, String size, String color) {
        if (type.equals("child")) {
            ChildCoat c = new ChildCoat();
            c.setSize(Objects.isNull(size) ? DEFAULT_SIZE : size);
            c.setColor(Objects.isNull(color) ? DEFAULT_COLOR : color);
            return c;
        }
        return null;
    }

    public static <T> Coat getCoat(Class<T> c, String size, String color) {
        try {
            //caifeng

            //模特



            Object object = c.newInstance();

            Method m1 = c.getMethod("setSize", String.class);
            Method m2 = c.getMethod("setColor", String.class);
            m1.invoke(object, size);
            m2.invoke(object, color);
            return (Coat)object;

        } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException | InstantiationException e) {
            e.printStackTrace();
        }
        return null;
    }


    public static void main(String[] args) {

//        SqlSessionFactory 接口 -DefaultSqlSessionFactory -
//        opensession () {
//            return new DefaultSqlSession(configuration, executor, autoCommit);
//        }

        Coat coat = CoatFactory.getCoat("child", null, null);
        System.out.println(coat.info());
        Coat coat1 = CoatFactory.getCoat("child", "xxxxxxl", "黄色");
        System.out.println(coat1.info());

        Coat coat2 = CoatFactory.getCoat(ChildCoat.class,"xxl","red");
        System.out.println(coat2.info());

    }

}
